<template>
	<view class="select-voucher" v-show="show">
		<uni-popup ref="popupRef" type="bottom" border-radius="10px 10px 0 0" background-color="#fff">
			<view class="select-voucher-content">
				<view class="select-voucher-header">
					选择核销券
				</view>
				<scroll-view class="scroll-Y" scroll-y="true">
					<view class="select-voucher-list">
						<view :class="['select-voucher-item', {
							'select-voucher-item-active': currentVoucher.voucherid == item.voucherid
						}]" v-for="item in list" :key="item.voucherid" @click="() => voucherSelect(item)">
							<u-image v-if="item.goodsimg" :src="item.goodsimg" mode="" width="120rpx" height="120rpx"
								border-radius="12"></u-image>
							<u-image v-else src="@/static/image/logo.png" mode="" width="120rpx" height="120rpx"
								border-radius="12"></u-image>
							<view class="select-voucher-item-text">
								{{item.goodsname}}
							</view>
						</view>
					</view>
				</scroll-view>
				<view class="select-voucher-footer">
					<u-button size="medium" @click="close">取消</u-button>
					<u-button type="error" @click="confirmVoucher" size="medium">确定</u-button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				list: [],
				scrollTop: 0,
				currentVoucher: {},
			}
		},
		methods: {
			openModal(voucher) {
				if (voucher) {
					this.currentVoucher = voucher
				}
				this.show = true
				this.$refs.popupRef.open('bottom')
				this.loadVoucher()
			},
			close() {
				this.show = false
				this.$refs.popupRef.close()
			},
			voucherSelect(item) {
				this.currentVoucher = this.currentVoucher.voucherid  == item.voucherid ? {} : item
			},
			loadVoucher() {
				this.$u.api
					.postFindVaildVoucherList({
						page: 1,
						rows: 999
					}).then(res => {
						this.list = res.data.rows
					})

			},
			confirmVoucher() {
				this.$emit('confirm', this.currentVoucher)
				this.close()
				this.currentVoucher = {}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.select-voucher-content {
		height: 75vh;
		display: flex;
		flex-direction: column;
	}

	.select-voucher-item {
		display: flex;
		padding: 26rpx;
		justify-content: space-between;
		box-shadow: 0px 3px 8px 0px rgba(0, 0, 0, 0.16);
		margin: 30rpx;
		border-radius: 24rpx;
		box-sizing: border-box;

		&-text {
			flex: 1;
			padding-left: 20rpx;
			font-size: 32rpx;
		}

		&-active {
			border: solid 1rpx red;
		}
	}

	.select-voucher-footer {
		width: 100%;
		display: flex;
		justify-content: space-around;
		height: 120rpx;
		align-items: center;
		background-color: #fff;
	}

	.scroll-Y {
		flex: 1
	}
	.select-voucher-header {
		text-align: center;
		font-size: 34rpx;
	}
</style>